<template>
  <div class="wrapper">
    <!-- header部分 -->
    <header>
      <div class="location-text"><Back></Back>我的收藏</div>
    </header>

    <!-- 收藏的商家列表部分 -->
    <ul class="business">
      <li v-for="item in favoriteBusinesses" :key="item.businessId" @click="toBusinessInfo(item.businessId)">
        <div class="business-img">
          <img :src="item.businessImg">
        </div>
        <div class="business-info">
          <h3>{{item.businessName}}</h3>
          <p>&#165;{{item.starPrice}}起送 | &#165;{{item.deliveryPrice}}配送</p>
          <p>{{item.businessExplain}}</p>
        </div>
      </li>
    </ul>

    <!-- 底部菜单部分 -->
    <Footer></Footer>
  </div>
</template>

<script>
// 导入共通组件
import Footer from '../components/Footer.vue';
import Back from '../components/Back.vue';
export default {
  name: 'MyFavorites',
  data() {
    return {
      user: {}, // 用户信息
      favoriteBusinesses: [] // 收藏的商家列表
    };
  },
  created() {
    // 从 sessionStorage 中获取用户信息
    this.user = this.$getSessionStorage('user');
    // 如果用户已登录，获取收藏的商家列表
    if (this.user) {
      this.listFavorites();
    }
  },
  components: {
    Footer,
	Back
  },
  methods: {
    // 获取用户收藏的商家列表
    listFavorites() {
      this.$axios.post('FavoriteController/listFavoritesByUserId', this.$qs.stringify({
        userId: this.user.userId
      })).then(response => {
        this.favoriteBusinesses = response.data;
      }).catch(error => {
        console.error(error);
      });
    },
    // 跳转到商家详情页
    toBusinessInfo(businessId) {
      this.$router.push({ path: '/businessInfo', query: { businessId: businessId } });
    }
  }
};
</script>

<style scoped>
/****************** 总容器 ******************/
.wrapper {
  width: 100%;
  height: 100%;
}

/****************** header ******************/
.wrapper header {
  width: 100%;
  height: 12vw;
  background-color: #0097FF;
  font-size: 4.8vw;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
}

/****************** 收藏的商家列表部分 ******************/
.wrapper .business {
  width: 100%;
  //margin-top: 12vw;
  //padding-bottom: 14vw;
}

.wrapper .business li {
  width: 100%;
  box-sizing: border-box;
  padding: 2.5vw;
  border-bottom: solid 1px #DDD;
  user-select: none;
  cursor: pointer;

  display: flex;
  align-items: center;
}

.wrapper .business li .business-img {
  position: relative;
}

.wrapper .business li .business-img img {
  width: 20vw;
  height: 20vw;
}

.wrapper .business li .business-info {
  margin-left: 3vw;
}

.wrapper .business li .business-info h3 {
  font-size: 3.8vw;
  color: #555;
}

.wrapper .business li .business-info p {
  font-size: 3vw;
  color: #888;
  margin-top: 2vw;
}
</style>
